<template>
    <div class="components_edit_modal">
        <el-dialog width="800px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="formInfo" :model="formInfo" :rules="formRules" label-width="95px" :inline="true" size="mini">
                <el-form-item label="店铺名称：" prop="name">
                    <el-input v-model.trim="formInfo.name" clearable placeholder="请输入店铺名称"></el-input>
                </el-form-item>
                <el-form-item label="店铺代码：" prop="code">
                    <el-input v-model.trim="formInfo.code" clearable placeholder="请输入店铺代码"></el-input>
                </el-form-item>
                <el-form-item label="联系电话：" prop="linkman">
                    <el-input v-model.trim="formInfo.linkman" clearable placeholder="请输入联系电话"></el-input>
                </el-form-item>
                <el-form-item label="店铺经度：" prop="longitude">
                    <el-input v-model.trim="formInfo.longitude" clearable placeholder="请输入店铺经度"></el-input>
                </el-form-item>
                <el-form-item label="店铺纬度：" prop="latitude">
                    <el-input v-model.trim="formInfo.latitude" clearable placeholder="请输入店铺纬度"></el-input>
                </el-form-item>
                <el-form-item label="所属地区：" prop="city">
                    <el-input v-model.trim="formInfo.city" placeholder="请选择店铺名称" style="display:none;"></el-input>
                    <el-cascader expand-trigger="hover" :options="options.cityInfoOptions" v-model="cityarr" filterable clearable :props="props" @change="handleChange">
                    </el-cascader>
                </el-form-item>
                <el-form-item label="所属区域：" prop="region">
                    <el-select v-model="formInfo.region" clearable filterable placeholder="请选择相册标题或者描述">
                        <el-option v-for="item in options.areaOptions" :key="item.label" :label="item.label" :value="item.label">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="店铺类型：" prop="typeId">
                    <el-select v-model="formInfo.typeId" clearable filterable placeholder="请选择店铺类型">
                        <el-option v-for="item in options.typeIdOptions" :key="item.id" :label="item.dictValue" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="系统店铺：" prop="isRoot">
                    <el-switch v-model="formInfo.isRoot" active-color="#13ce66" inactive-color="#ff4949">
                    </el-switch>
                </el-form-item>
                <el-form-item label="封面上传：" prop="avatarUrl" class="inline-block">
                    <el-input v-model.trim="formInfo.avatarUrl" clearable style="display:none;"></el-input>
                    <pl-upload :postData="attachment" :api="'jstn-pic'" :displaymode="'images'" :imageOnly="false" :buttonIds='{select:"selectfiles1",upload:"postfiles1",container:"container1"}' @reloadFiles="reloadFiles"></pl-upload>
                </el-form-item>
                <el-form-item label="详细地址：" prop="address" class="inline-block">
                    <el-input type="textarea" v-model.trim="formInfo.address" clearable placeholder="请输入详细地址"></el-input>
                </el-form-item>
                <el-form-item label="店铺详情：" prop="description" class="inline-block">
                    <el-input type="textarea" v-model.trim="formInfo.description" clearable placeholder="请输入店铺详情"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="mini" @click="addInfo('formInfo')">确 定</el-button>
                <el-button @click="state = false" size="mini">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import cityInfo from '../../../cityInfo' //  城市列表
    import { enums } from '@/constant'
    import { mapGetters } from 'vuex'
    import PlUpload from "../../../common/PlUpload"
    export default {
        components: {
            ...mapGetters({
                stat: 'getUserList_No',
            }),
            "pl-upload": PlUpload
        },
        data() {
            return {
                state: false,
                title: "",
                type: "",
                imageUrl: "",
                loading: false,
                attachment: [], //上传附件
                value: "",
                formInfo: {
                    name: null,
                    address: null,
                    avatarUrl: null,
                    city: null,
                    code: null,
                    description: null,
                    district: null,
                    isRoot: false,
                    latitude: null,
                    linkman: null,
                    longitude: null,
                    name: null,
                    province: null,
                    region: null,
                    typeId: null
                },
                formRules: {
                    name: [{
                        required: true,
                        message: "请输入店铺名称",
                        trigger: "blur"
                    }],
                    address: [{
                        required: true,
                        message: "请输入店铺地址",
                        trigger: "blur"
                    }],
                    avatarUrl: [{
                        required: true,
                        message: "请输入店铺封面图片",
                        trigger: "blur"
                    }],
                    city: [{
                        required: true,
                        message: "请选择店铺所属城市",
                        trigger: "blur"
                    }],
                    code: [{
                            required: true,
                            message: "请输入店铺代码",
                            trigger: "blur"
                        },
                        {
                            min: 0,
                            max: 5,
                            message: '长度在 0 到 5 个字符',
                            trigger: 'blur'
                        }
                    ],
                    linkman: [{
                        required: true,
                        message: "请输入店铺联系电话",
                        trigger: "blur"
                    }],
                    region: [{
                        required: true,
                        message: "请选择店铺所属区域",
                        trigger: "blur"
                    }],
                    description: [{
                        required: true,
                        message: "请输入奖品描述",
                        trigger: "blur"
                    }]
                },
                cityarr: [],
                options: {
                    albumOptions: [],
                    topicOptions: [],
                    userOptions: [],
                    areaOptions: [{
                            label: '华东地区'
                        },
                        {
                            label: '华南地区'
                        },
                        {
                            label: '华中地区'
                        },
                        {
                            label: '华北地区'
                        },
                        {
                            label: '东北地区'
                        },
                        {
                            label: '西北地区'
                        },
                        {
                            label: '西南地区'
                        },
                        {
                            label: '港澳台地区'
                        },
                    ]
                },
                props: {
                    value: 'name',
                    label: 'name',
                    children: 'sub'
                },
                attachment: [], //上传附件
            };
        },
        created() {
            this.getShopTypeList();
        },
        mounted() {
        },
        methods: {
            showDialog(val) {
                // console.log(val);
                if (this.$refs["formInfo"] !== undefined) {
                    this.$refs["formInfo"].resetFields('');
                }
                this.formInfo = {};
                if (val) {
                    for (var key in val) {
                        this.formInfo[key] = val[key];
                    }
                    this.title = "编辑店铺";
                    this.type = "edit";
                } else {
                    this.title = "添加店铺";
                    this.type = "add";
                }
                this.state = true;
                this.options.cityInfoOptions = cityInfo.cityInfo;
                console.log(cityInfo.cityInfo);
            },
            // 选择城市
            handleChange(value) {
                if (value) {
                    this.formInfo.province = value[0];
                    this.formInfo.city = value[1];
                    this.formInfo.district = value[2];
                }
            },
            // 获取店铺类型数据
            getShopTypeList() {
                let vm = this;
                vm.axios.get(api.dictionary.dictList + "?type=shop").then((response) => {
                    if (response.data.code == 200) {
                        console.log(response.data.data.dataList);
                        vm.options.typeIdOptions = response.data.data.dataList;
                    } else {
                        Message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
            addInfo() {
                let vm = this;
                vm.$refs.formInfo.validate(valid => {
                    if (valid) {
                        let submission = ""; //提交方式
                        //选择提交方式
                        if (vm.type == "edit") {
                            submission = "put";
                        } else {
                            submission = "post";
                        }
                        // 提交
                        this.axios({
                            method: submission,
                            url: api.shopsManagement.shopsList,
                            data: vm.formInfo
                        }).then(response => {
                            if (response.data.code == 200) {
                                vm.state = false;
                                vm.$parent.$refs.tableList.$emit("vuetable:refresh");
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            reloadFiles: function(files) {
                let vm = this;
                this.attachment = [];
                files.forEach((item, idnex) => {
                    item.url = item.fileStoreName;
                    this.attachment.push(item)
                })
                this.formInfo.avatarUrl = this.attachment[0].url;
            },
            // 格式化 url 地址
            urlFormat(strUrl) {
                // var strUrl = "https://jstn-pic.oss-cn-qingdao.aliyuncs.com/d16e8272-4ac7-de6a-1071-a79ded59d29a.jpg"
                var arrUrl = strUrl.split("//")[1].split("/");
                var objectKey = arrUrl[1];
                var bucket = arrUrl[0].split(".")[0];
                var fileType = arrUrl[1].split(".")[1];
                var resultObject = {
                    bucket: bucket,
                    objectKey: objectKey,
                    fileType: fileType
                }
                return resultObject
            }
        }
    };
</script>
<style scoped>

</style>